const path = require("path");
function resolve(dir) {
    return path.join(__dirname, dir);
}

// cdn load
// const isProd = process.env.NODE_ENV === 'production';
// const cdn = {
//     css: [],
//     js: [
//         'https://cdn.bootcss.com/vue/2.5.17/vue.runtime.min.js',
//         'https://cdn.bootcss.com/vue-router/3.0.1/vue-router.min.js',
//         'https://cdn.bootcss.com/vuex/3.0.1/vuex.min.js',
//         'https://cdn.bootcss.com/axios/0.18.0/axios.min.js',
//         'https://cdn.bootcss.com/vue-i18n/7.6.0/vue-i18n.min.js',
//         'https://cdn.jsdelivr.net/npm/vant@2.4/lib/vant.min.js',
//         'https://cdn.bootcss.com/less.js/3.10.3/less.js'
//     ]   
// }

// compress
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const compress = new CompressionWebpackPlugin(
    {
        filename: info => {
            return `${info.path}.gz${info.query}`
        },
        algorithm: 'gzip',
        threshold: 10240,
        test: new RegExp(
            '\\.(' +
            ['js'].join('|') +
            ')$'
        ),
        minRatio: 0.8,
        deleteOriginalAssets: false
    }
)

// vue.config.js
module.exports = {
    publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
    outputDir: "dist",
    assetsDir: "assets",
    filenameHashing: true,
    lintOnSave: false,
    productionSourceMap: false,
    css: {
        loaderOptions: {
            less: {
                modifyVars: {
                    'hack': `true; @import "${resolve('src/assets/css/default.less')}";`
                }
            }
        }
    },
    devServer: {
        host: "0.0.0.0",
        port: 8081,
        https: false,
        open: false,
        proxy: {
            "/": {
                target: "http://i.itpk.cn",
                ws: false,
                changeOrigin: true,
                pathRewrite: {
                    '^/': '/'
                }
            }
        },
        before(app) {
            app.get(/.*.(js)$/, (req, res, next) => {
                req.url = req.url + '.gz';
                res.set('Content-Encoding', 'gzip');
                next();
            })
        },
    },
    chainWebpack: config => {
        // CDN 
        // 移除 prefetch 插件
        // config.plugins.delete('prefetch')
        // 移除 preload 插件
        // config.plugins.delete('preload');
        config.resolve.alias
            .set("@", resolve("src"))
            .set("utils", resolve("src/utils"))
            .set("assets", resolve("src/assets"));
        config.optimization.minimize(true);
        config.optimization.splitChunks({
            chunks: 'all'
        });
        config.module
            .rule('images')
            .use('image-webpack-loader')
            .loader('image-webpack-loader')
            .options({
                bypassOnDebug: true
            })
            .end();
        // CDN
        // if (isProd) {
        //     config.plugin('html')
        //         .tap(args => {
        //             args[0].cdn = cdn;
        //             return args;
        //         });
        // }
    },
    configureWebpack: config => {
        // if (isProd) {
        //     // CDN
        //     config.externals = {
        //         'vue': 'Vue',
        //         'vuex': 'Vuex',
        //         'vue-router': 'VueRouter',
        //         'axios': 'axios',
        //         'vue-i18n': 'VueI18n',
        //         'vant': 'Vant',
        //         'less': 'less'
        //     }
        // };
        config.plugins.push(compress)
    }
}